/*
   Copyright (C) 2011 Roman Belov <romblv@gmail.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _H_LEXER_
#define _H_LEXER_

#include <stdio.h>

#include "util/list.h"

#define LEXER_SYM_MAX			(80)

enum {
	TOKEN_NULL,
	TOKEN_INVAL,
	TOKEN_USE,		/* use				*/
	TOKEN_MODULE,		/* module			*/
	TOKEN_PORT,		/* port				*/
	TOKEN_PLACE,		/* place			*/
	TOKEN_LINK,		/* link				*/
	TOKEN_IN,		/* in				*/
	TOKEN_OUT,		/* out				*/
	TOKEN_INOUT,		/* inout			*/
	TOKEN_LIB,		/* lib				*/
	TOKEN_SPECIFY,		/* specify			*/
	TOKEN_LEFT_BRACKET,	/* {				*/
	TOKEN_RIGHT_BRACKET,	/* }				*/
	TOKEN_DOT,		/* .				*/
	TOKEN_EQUALITY,		/* =				*/
	TOKEN_PLUS,		/* +				*/
	TOKEN_MINUS,		/* -				*/
	TOKEN_SEMICOLON,	/* ;				*/
	TOKEN_SYMBOL,		/* [a-zA-Z_][a-zA-Z0-9_]*	*/
	TOKEN_STRING,		/* \"[^"]*\"			*/
	TOKEN_NUMBER,		/* [0-9]+			*/
};

struct token {
	int	id;		/* token identifier */
	char	*val;		/* related value (attribute) */
};

struct lexer {
	FILE		*fd;		/* file descriptor */
	char		*fname;		/* file name */
	struct token	tok;		/* current token */
	char		*buff;		/* input buffer */
	char		*bp;		/* buffer pointer */
	int		line;		/* current line number */
	char		*attr;		/* buffer for attributes */
	int		btok;		/* broken token flag */
	int		error;		/* error number */
};

struct lexer_global {
	list_node_t	path_list;	/* list of paths */
};

extern struct lexer_global lexglob;

struct lexer *
lexer_source_open(const char *file,
		const char *from_file,
		int line);

void
lexer_source_close(struct lexer *lex);

void
lexer_token_next(struct lexer *lex);

#endif /* _H_LEXER_ */

